package com.club.subject.application.controller;

import com.sfy.auth.entity.Result;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

public class SubjectLikedController {


    RedisTemplate redisTemplate = new RedisTemplate();

    private class SubjectLikedDTO {

    }

    //private RedisUtil redisUtil;
    public Result<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        //拿到点赞人的信息
        //String loginId = LoginUtil.getLoginId();
        return Result.OK();

    }

    //通过xxl-job做数据同步,现将数据抽取出来
    public Map<Object, Object> getHashAndDelete(String key) {
        Map<Object, Object> map = new HashMap<>();
        Cursor<Map.Entry<Object, Object>> scan = redisTemplate.opsForHash().scan(key, ScanOptions.scanOptions().build());
        while (scan.hasNext()) {
            Map.Entry<Object, Object> entry = scan.next();
            String hashKey = (String) entry.getKey();
            Integer value = (Integer) entry.getValue();
            map.put(hashKey, value);
            redisTemplate.opsForHash().delete(key, hashKey);
        }
        return map;
    }
}
